﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WarstwaDanych;
using UdostepniaczPlikow.ViewModels;
using System.Configuration;
using System.IO;

namespace UdostepniaczPlikow.Controllers
{
   // [Authorize]
    public class FileController : Controller
    {
        //
        // GET: /File/

        public List<FileViewModel> files = new List<FileViewModel>() { 
                                  new FileViewModel() { Nazwa = "Plik A", Sciezka = @"~\Pliki\a.txt", Size = 0 },
                                  new FileViewModel() { Nazwa = "Plik B", Sciezka = @"~\Pliki\b.txt", Size = 0 },
                                  new FileViewModel() { Nazwa = "Plik C", Sciezka = @"~\Pliki\c.txt", Size = 0 },
                                  new FileViewModel() { Nazwa = "Plik D", Sciezka = @"~\Pliki\d.txt", Size = 0 },
                               };

        List<string> usersList = new List<string>{
                                                "User1",
                                                "User2",
                                                "User3",
                                                "User4"};
                

        List<string> UsersWithAccess = new List<string>();

        //MultiSelectList lista1 = new MultiSelectList(usersList);
        //MultiSelectList lista2 = new MultiSelectList(UsersWithAccess);
                

        public ActionResult Index()
        {
            //// ASPNETDBEntities baza = new ASPNETDBEntities();
            //WarstwaDanych.Model1Container baza = new Model1Container(ConfigurationManager.ConnectionStrings["Model1Container"].ConnectionString);
            //var resultViewModel = baza.Pliki
            //    // .Where( p=> p.Uprawnienia_do_pliku.aspnet_Users.UserName == User.Identity.Name)
            //    .Select(p => new FileViewModel() { Name = p.Sciezka, Path = p.Sciezka, Size = 0 }).ToList();

            var resultViewModel = files;
          
            return View(resultViewModel);
        }
        
        [HttpGet]
        public ActionResult Create()
        {
            FileViewModel viewModel = new FileViewModel() ;
            return View(viewModel);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(HttpPostedFileBase uploadFile)
        {
            
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/Pliki"),
                                               Path.GetFileName(uploadFile.FileName));
                uploadFile.SaveAs(filePath);

                files.Add(new FileViewModel() {Id = 1234, Nazwa = uploadFile.FileName, Sciezka = filePath , Size = 0} );
            
            
            return View();

        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            FileViewModel viewModel = new FileViewModel() { Nazwa = "Sciezka" };
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(int id, FileViewModel viewModel)
        {
            var s = viewModel.Nazwa;
            return View(viewModel);
        }

        //public ActionResult Uprawnienia(FileViewModel viewModel)
        //{
            
        //    return View(viewModel);
        //}

        public ActionResult Delete(FileViewModel viewModel)
        {
            var resultViewModel = files;

            return View(resultViewModel);
        }

        public ActionResult AdminDelete(FileViewModel viewModel)
        {
            var resultViewModel = files;

            return View(resultViewModel);
        }

        [HttpGet]
        public ActionResult Uprawnienia()
        {
            UserSelectionViewModel viewModel = new UserSelectionViewModel();
            viewModel.AvailableUsers = usersList;
            viewModel.SelectedUsers = UsersWithAccess;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult UprawnieniaDodaj(string[] usersi)
        {
            UsersWithAccess.AddRange(usersi);
            return View(Uprawnienia());
        }

        [HttpPost]
        public ActionResult UprawnieniaUsun(UserSelectionViewModel viewModel)
        {

            return Uprawnienia();
        }

        public FileResult Pobierz(string path)
        {
            string contentType = "text/plain";
            string name = path.Substring(path.LastIndexOf('\\')+1);
            return File(path, contentType, name);
        }

        
    }
}
